guard, when, and unless allow for conditional operations and short-cutting in monadic expressions.
Guards
Guards are used to stop the monadic expression continuing if a flag is true (for guard) or false (for guardnot).
They only work with monads that have an 'alternative value' (which is usually used as the error condition: Left in
Either for example). An alternative value is provided when the guard triggers:
from x in ma
from _ in guard(x == 100, Error.New("x should be 100"))
select x;
Supported monads are:
Either
EitherUnsafe
EitherAsync
Fin
Validation
Aff
Eff
When and Unless
when and unless are similar to guards, but instead of providing the alternative value, you provide an alternative monad
to run. This monad could be in a failed state, or it could run a successful side effect (an Eff calling Console<RT>.writeLine()
for example).
from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;
Contents
- Prelude
- guard <F> (bool flag)
- guardIO (bool flag)
- guard <E> (bool flag, Func<E> False)
- guard <E> (bool flag, E False)
- guard (bool flag, Func<Error> False)
- guard (bool flag, Error False)
- Prelude
- guardnot <E> (bool flag, Func<E> True)
- guardnot <E> (bool flag, E True)
- guardnot (bool flag, Func<Error> True)
- guardnot (bool flag, Error True)
- Prelude
- iff <M, A> (K<M, bool> Pred, K<M, A> Then, K<M, A> Else)
- iff <M, A> (K<M, bool> Pred, K<M, A> Then, K<IO, A> Else)
- iff <M, A> (K<M, bool> Pred, K<IO, A> Then, K<M, A> Else)
- iff <M, A> (K<M, bool> Pred, K<IO, A> Then, K<IO, A> Else)
- iff <M, A> (K<M, bool> Pred, K<M, A> Then, Pure<A> Else)
- iff <M, A> (K<M, bool> Pred, Pure<A> Then, K<M, A> Else)
- iff <M, A> (K<M, bool> Pred, Pure<A> Then, Pure<A> Else)
- iff <M, A> (K<M, bool> Pred, Pure<A> Then, K<IO, A> Else)
- iff <M, A> (K<M, bool> Pred, K<IO, A> Then, Pure<A> Else)
- iff <F, A> (bool Pred, K<F, A> Then, K<F, A> Else)
- Prelude
- unless <F> (bool flag, K<F, Unit> alternative)
- unless <M> (K<M, bool> Pred, K<M, Unit> Then)
- unless <M> (K<M, bool> Pred, K<IO, Unit> Then)
- unless <M> (K<M, bool> Pred, Pure<Unit> Then)
- Prelude
Methods
method K<F, Unit> guard <F> (bool flag) Source #
Guard against continuing an applicative expression
Parameters
| param | flag | Flag for continuing |
| returns | Applicative that yields | |
method IO<Unit> guardIO (bool flag) Source #
Guard against continuing an applicative expression
Parameters
| param | flag | Flag for continuing |
| returns | Applicative that yields | |
method Guard<E, Unit> guard <E> (bool flag, Func<E> False) Source #
Guard against continuing a monadic expression
Parameters
| param | flag | Flag for continuing |
| param | False | If the flag is false, this provides the error |
| returns | Guard | |
method Guard<E, Unit> guard <E> (bool flag, E False) Source #
Guard against continuing a monadic expression
Parameters
| param | flag | Flag for continuing |
| param | False | If the flag is false, this provides the error |
| returns | Guard | |
Methods
method Guard<E, Unit> guardnot <E> (bool flag, Func<E> True) Source #
Guard against continuing a monadic expression
Parameters
| param | flag | Flag for continuing |
| param | True | If the flag is false, this provides the error |
| returns | Guard | |
method Guard<E, Unit> guardnot <E> (bool flag, E True) Source #
Guard against continuing a monadic expression
Parameters
| param | flag | Flag for continuing |
| param | True | If the flag is false, this provides the error |
| returns | Guard | |
Methods
method K<M, A> iff <M, A> (K<M, bool> Pred, K<M, A> Then, K<M, A> Else) Source #
Compute the predicate and depending on its state compute Then or Else
Parameters
| type | M | Monad |
| param | Pred | Predicate |
| param | Then | Computation |
| param | Else | Computation |
| returns | Unit monad | |
method K<M, A> iff <M, A> (K<M, bool> Pred, K<M, A> Then, K<IO, A> Else) Source #
Compute the predicate and depending on its state compute Then or Else
Parameters
| type | M | Monad |
| param | Pred | Predicate |
| param | Then | Computation |
| param | Else | Computation |
| returns | Unit monad | |
method K<M, A> iff <M, A> (K<M, bool> Pred, K<IO, A> Then, K<M, A> Else) Source #
Compute the predicate and depending on its state compute Then or Else
Parameters
| type | M | Monad |
| param | Pred | Predicate |
| param | Then | Computation |
| param | Else | Computation |
| returns | Unit monad | |
method K<M, A> iff <M, A> (K<M, bool> Pred, K<IO, A> Then, K<IO, A> Else) Source #
Compute the predicate and depending on its state compute Then or Else
Parameters
| type | M | Monad |
| param | Pred | Predicate |
| param | Then | Computation |
| param | Else | Computation |
| returns | Unit monad | |
method K<M, A> iff <M, A> (K<M, bool> Pred, K<M, A> Then, Pure<A> Else) Source #
Compute the predicate and depending on its state compute Then or Else
Parameters
| type | M | Monad |
| param | Pred | Predicate |
| param | Then | Computation |
| param | Else | Computation |
| returns | Unit monad | |
method K<M, A> iff <M, A> (K<M, bool> Pred, Pure<A> Then, K<M, A> Else) Source #
Compute the predicate and depending on its state compute Then or Else
Parameters
| type | M | Monad |
| param | Pred | Predicate |
| param | Then | Computation |
| param | Else | Computation |
| returns | Unit monad | |
method K<M, A> iff <M, A> (K<M, bool> Pred, Pure<A> Then, Pure<A> Else) Source #
Compute the predicate and depending on its state compute Then or Else
Parameters
| type | M | Monad |
| param | Pred | Predicate |
| param | Then | Computation |
| param | Else | Computation |
| returns | Unit monad | |
method K<M, A> iff <M, A> (K<M, bool> Pred, Pure<A> Then, K<IO, A> Else) Source #
Compute the predicate and depending on its state compute Then or Else
Parameters
| type | M | Monad |
| param | Pred | Predicate |
| param | Then | Computation |
| param | Else | Computation |
| returns | Unit monad | |
method K<M, A> iff <M, A> (K<M, bool> Pred, K<IO, A> Then, Pure<A> Else) Source #
Compute the predicate and depending on its state compute Then or Else
Parameters
| type | M | Monad |
| param | Pred | Predicate |
| param | Then | Computation |
| param | Else | Computation |
| returns | Unit monad | |
method K<F, A> iff <F, A> (bool Pred, K<F, A> Then, K<F, A> Else) Source #
If this then that for higher-kinds
Parameters
| type | F | Trait type |
| type | A | Bound return value |
| param | Pred | Boolean flag to be computed |
| param | Then | Then branch if the flag computes to |
| param | Else | Else branch if the flag computes to |
| returns | Returns either the | |
Methods
method K<F, Unit> unless <F> (bool flag, K<F, Unit> alternative) Source #
Conditional execution of Applicative expressions
Run the alternative when the flag is false, return pure () when true
Parameters
| param | flag | If |
| param | alternative | Computation to run if the flag is |
| returns | Either the result of the | |
Examples
from x in ma
from _ in unless(x == 100, Console.writeLine〈RT〉("x should be 100!"))
select x;
method K<M, Unit> unless <M> (K<M, bool> Pred, K<M, Unit> Then) Source #
When the predicate evaluates to false, compute Then
Parameters
| type | M | Monad |
| param | Pred | Predicate |
| param | Then | Computation |
| returns | Unit monad | |
Methods
method K<F, Unit> when <F> (bool flag, K<F, Unit> alternative) Source #
Conditional execution of Applicative expressions
Run the alternative when the flag is true, return pure () when false
Parameters
| param | flag | If |
| param | alternative | Computation to run if the |
| returns | Either the result of the | |
Examples
from x in ma
from _ in when(x == 100, Console.writeLine〈RT〉("x is 100, finally!"))
select x;
method K<M, Unit> when <M> (K<M, bool> Pred, K<M, Unit> Then) Source #
When the predicate evaluates to true, compute Then
Parameters
| type | M | Monad |
| param | Pred | Predicate |
| param | Then | Computation |
| returns | Unit monad | |